import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class PeerHandler implements Runnable {
    private Socket socket;
    private Socket peerSocket;
    private BufferedReader input;
    private PrintWriter output;
    private ClientHandler ch;

    public PeerHandler(ClientHandler ch, Socket socket, Socket peerSocket) throws IOException {
        this.socket = socket;
        this.peerSocket = peerSocket;
        this.ch = ch;
        input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        output = new PrintWriter(peerSocket.getOutputStream(), true);
    }

    @Override
    public void run() {
        try {
            while (true) {
                // 传递匹配的两个客户端信息
                String message = input.readLine();
                output.println(message);
                if(message.equals("9999999")){
                    socket.close();
                    peerSocket.close();
                    ch.releaseRoom();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
